package com.beardedhen.harmonybootstrap;

import com.beardedhen.harmonybootstrap.api.attributes.BootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.view.BootstrapBadgeView;
import com.beardedhen.harmonybootstrap.api.view.BootstrapBrandView;
import com.beardedhen.harmonybootstrap.api.view.BootstrapSizeView;
import com.beardedhen.harmonybootstrap.utils.DimenUtils;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.utils.ViewUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.app.Context;

import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;

import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * See
 *
 * <a href="http://getbootstrap.com/components/#badges">http://getbootstrap.com/components/#badges</a>
 */
public class BootstrapBadge extends Image implements BootstrapSizeView, BootstrapBrandView,
        BootstrapBadgeView {

    private String badgeText;
    private int size;
    private float bootstrapSize;
    private static final String BOOTSTRAP_BADGE_BOOTSTRAP_SIZE ="bootstrapSize";
    private static final String BOOTSTRAP_BADGE_BADGE_TEXT ="badgeText";
    private Element badgeDrawable;
    private boolean insideContainer;
    private BootstrapBrand bootstrapBrand = DefaultBootstrapBrand.REGULAR;;


    public BootstrapBadge(Context context) {
        super(context);
        init(null);
    }

    public BootstrapBadge(Context context, AttrSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public BootstrapBadge(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttrSet attrs)  {
        int sizeOrdinal = -1;
        if (attrs != null) {
            if (attrs.getAttr(BOOTSTRAP_BADGE_BOOTSTRAP_SIZE).isPresent()) {
                String sizeOrdinalStr = attrs.getAttr(BOOTSTRAP_BADGE_BOOTSTRAP_SIZE).get().getStringValue().trim();
                sizeOrdinal = UiUtil.getBootstrapSizeInt(sizeOrdinalStr);
            }
            if (badgeText == null) {
                badgeText = attrs.getAttr(BOOTSTRAP_BADGE_BADGE_TEXT).isPresent() ? attrs.getAttr(
                    BOOTSTRAP_BADGE_BADGE_TEXT).get().getStringValue() : "badge";
            }
        } else {
            badgeText = "BootStrap";
        }

        bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal).scaleFactor();

        try {
            size = (int) DimenUtils.pixelsFromDpResource(getContext(), ResourceTable.Float_bootstrap_badge_default_size);
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        updateBootstrapState();
    }

    private void updateBootstrapState() {
        badgeDrawable = BootstrapDrawableFactory.createBadgeDrawable(getContext(), bootstrapBrand,
                (int) (size * bootstrapSize),
                (int) (size * bootstrapSize),
                badgeText,
                insideContainer);

        ViewUtils.setBackgroundDrawable(this, badgeDrawable);
    }

    Element getBadgeDrawable() {
        return badgeDrawable;
    }

    @Override
    public String getBadgeText() {
        return badgeText;
    }

    @Override
    public void setBadgeText(String badgeText) {
        this.badgeText = badgeText;
        updateBootstrapState();
    }

    public void setBootstrapBrand(BootstrapBrand bootstrapBrand, boolean insideContainer) {
        this.insideContainer = insideContainer;
        setBootstrapBrand(bootstrapBrand);
    }

    @Override
    public void setBootstrapBrand(BootstrapBrand bootstrapBrand) {
        this.bootstrapBrand = bootstrapBrand;
        updateBootstrapState();
    }

    @Override
    public BootstrapBrand getBootstrapBrand() {
        return bootstrapBrand;
    }

    @Override
    public void setBootstrapSize(DefaultBootstrapSize bootstrapSize) {
        this.bootstrapSize = bootstrapSize.scaleFactor();
        updateBootstrapState();
    }

    @Override
    public void setBootstrapSize(float bootstrapSize) {
        this.bootstrapSize = bootstrapSize;
    }

    @Override
    public float getBootstrapSize() {
        return bootstrapSize;
    }
}
